package com.open.party.cloud.view.home.jiCengDangJian.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewBaseFragment
import com.bumptech.glide.Glide
import com.open.party.cloud.R
import com.open.party.cloud.model.ZyzJiFenBean
import com.open.party.cloud.view.home.jiCengDangJian.partyService.ZyzJiFenMainActivity
import com.open.party.cloud.view.home.jiCengDangJian.adapter.ZyzJiFenListAdapter
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.view.image.rounded.RoundedImageView
import com.sinothk.widget.loadingRecyclerView.extend.LoadingRecycleViewHeader
import kotlinx.android.synthetic.main.zyz_fragment_ji_fen_list.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class ZyzJiFenBaseListFragment : AppRecycleViewBaseFragment<ZyzJiFenBean>() {

    protected var orgId = ""

    protected var ziYuanZheVM: ZiYuanZheViewModel? = null
    private var currView: View? = null
    private var adapter: ZyzJiFenListAdapter? = null
    private var parentActivity: ZyzJiFenMainActivity? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        parentActivity = context as ZyzJiFenMainActivity
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.zyz_fragment_ji_fen_list, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        ziYuanZheVM = ZiYuanZheViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)
        loadingRecyclerView.addHeaderView(createHeaderView())

        adapter = ZyzJiFenListAdapter(activity)
        loadingRecyclerView.adapter = adapter

        loadingRecyclerView.setPullRefreshEnabled(false)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        refreshData()
    }

    fun updateData(orgId: String) {
        this.orgId = orgId
        refreshData()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ZyzJiFenBean>>) {
        if (result.eventType != "getZyzJiFenOrderListData") {
            return
        }
        hideLoadingDialog()
        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<ZyzJiFenBean>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                val size = appPageData.records!!.size
                if (size > 3) {
                    showHeaderView(appPageData.records!!.subList(0, 3))
                    adapter!!.setData(appPageData.records!!.subList(3, size))
                } else {
                    showHeaderView(appPageData.records!!)
                    adapter!!.setData(ArrayList())
                }

                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }

    }

    private fun showHeaderView(records: List<ZyzJiFenBean>) {
        when (records.size) {
            1 -> {
                // 1
                showOrderView(records[0], image01View!!, name01Tv!!, score01Tv!!)
                showOrderView(null, image02View!!, name02Tv!!, score02Tv!!)
                showOrderView(null, image03View!!, name03Tv!!, score03Tv!!)
            }
            2 -> {
                showOrderView(records[0], image01View!!, name01Tv!!, score01Tv!!)
                showOrderView(records[1], image02View!!, name02Tv!!, score02Tv!!)
                showOrderView(null, image03View!!, name03Tv!!, score03Tv!!)
            }
            else -> {
                showOrderView(records[0], image01View!!, name01Tv!!, score01Tv!!)
                showOrderView(records[1], image02View!!, name02Tv!!, score02Tv!!)
                showOrderView(records[2], image03View!!, name03Tv!!, score03Tv!!)
            }
        }
    }

    private fun showOrderView(data: ZyzJiFenBean?, image01View: RoundedImageView, name01Tv: TextView, score01Tv: TextView) {
        if (data == null) {
            image01View.setImageResource(R.drawable.icon_default_img)
            name01Tv.text = "无"
            score01Tv.text = "0"
        } else {
            Glide.with(context!!).load(data.photo).placeholder(R.drawable.icon_default_img).into(image01View)
            name01Tv.text = XUtils.string().getNotNullValue(data.userName)
            score01Tv.text = data.score.toString()
        }
    }

    private fun createHeaderView(): View {
        val headerView: View = LoadingRecycleViewHeader.getViewByLayoutId(activity, R.layout.zyz_ji_fen_header_view)
        image01View = headerView.findViewById(R.id.image01View)
        name01Tv = headerView.findViewById(R.id.name01Tv)
        score01Tv = headerView.findViewById(R.id.score01Tv)

        image02View = headerView.findViewById(R.id.image02View)
        name02Tv = headerView.findViewById(R.id.name02Tv)
        score02Tv = headerView.findViewById(R.id.score02Tv)

        image03View = headerView.findViewById(R.id.image03View)
        name03Tv = headerView.findViewById(R.id.name03Tv)
        score03Tv = headerView.findViewById(R.id.score03Tv)
        return headerView
    }

    private var image01View: RoundedImageView? = null
    private var name01Tv: TextView? = null
    private var score01Tv: TextView? = null

    private var image02View: RoundedImageView? = null
    private var name02Tv: TextView? = null
    private var score02Tv: TextView? = null

    private var image03View: RoundedImageView? = null
    private var name03Tv: TextView? = null
    private var score03Tv: TextView? = null


}